package org.lhq.feign;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.lhq.entity.User;
import org.lhq.entity.UserFile;
import org.lhq.service.UserFileService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: admin-service
 * @description: 对UserFile外提供服务
 * @author: Wang defa
 * @create: 2021-01-05 19:36
 */

@RestController
@RequestMapping("file-feign")
@Slf4j
public class UserFileFeign {
    @Resource
    private UserFileService userFileService;
    @GetMapping("{id}")
    public UserFile getUserFileById(@PathVariable("id") Long id){
        return userFileService.getUserFileDao().selectById(id);
    }
    @GetMapping("user/{userId}")
    public List<UserFile> getUserFileListByUserId(@PathVariable("userId") Long userId){
    	return userFileService.getUserFileFileByUserId(userId);
	}
    @PostMapping("delete")
    public void deleteUserFileById(@RequestParam Long id){
        userFileService.getUserFileDao().deleteById(id);
    }
    @PostMapping("update")
    public void updateUserFile(@RequestBody UserFile userFile){
        userFileService.getUserFileDao().updateById(userFile);
    }
    @PostMapping("save")
    public void saveUserFile(@RequestBody UserFile userFile){
        userFileService.save(userFile);
    }
    @PostMapping("batchDeleteTable")
    Boolean batchDeleteTable(@RequestParam("ids") List<Long> ids){
        return this.userFileService.removeByIds(ids);
    }
    @PostMapping("page")
    IPage<UserFile> getUserPage(@RequestBody UserFile file,
                               @RequestParam(required = false) Long size,
                               @RequestParam(required = false) Long pageNum){
        pageNum = pageNum ==null?1:pageNum;
        size = size == null?5:size;
        log.info("查找条件:{}",file);
        log.info("查找第几页:{}",pageNum);
        log.info("查找每页大小:{}",size);
        QueryWrapper<UserFile> userFileQueryWrapper = null;
        if (file!=null&& StrUtil.isNotEmpty(file.getFileName())){
            userFileQueryWrapper = new QueryWrapper<>(file);
        }
        IPage<UserFile> userFilePage = new Page<UserFile>().setSize(size).setCurrent(pageNum);
        return userFileService.page(userFilePage, userFileQueryWrapper);
    }
}
